// pages/login/login.js
var common = require("../../utils/public.js") //引入public.js脚本
const config = require(`../../config.js`);
let api = require('../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //城市
    city:'',
    //微信头像
    avatarUrl:'',
    //省份
    province:'',
    //性别
    gender:'',
    //微信名字
    nickName:'',
    //用户id
    referee:'',
    //城市id
    cityId:''
  },
 
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   console.log(options)
    let that=this
    that.setData({
      city: options.city,
      avatarUrl: options.avatarUrl,
      province: options.province,
      gender: options.gender,
      nickName: options.nickName,
      referee: options.referee,
      cityId: options.cityId
    })

   
  },
  onShow: function () {

  },
  // 获取手机号
  getPhoneNumber: common.throttleFunc(function (e) {
    console.log(e)
    let that=this

    //允许
    if (e.detail.errMsg =='getPhoneNumber:ok'){
      let code=""
      wx.login({  
        success: res => {
          wx.request({
            url: `${config.url}/admin/user/login`,
            data: {
              wxCode: res.code,
              city: wx.getStorageSync("userMessage").city == "" ? '-1' : wx.getStorageSync("userMessage").city,
              province: wx.getStorageSync("userMessage").province == "" ? '-1': wx.getStorageSync("userMessage").province,
              imgurl: wx.getStorageSync("userMessage").avatarUrl,
              loginType: '1',
              sex: wx.getStorageSync("userMessage").gender,
              nickname: wx.getStorageSync("userMessage").nickName,
              type: '-1',
              iv: e.detail.iv,
              encryptedData: e.detail.encryptedData,
              referee: that.data.referee == 'undefined' ? '' : that.data.referee,  // 注册人id
              // referee:'',
              // cityId:'',
               cityId: that.data.cityId == 'undefined' ? '' : that.data.cityId, //城市id
              shopId:'-1',
              authUserId:'-1'
            },
            header: {
              'content-type': 'application/json',
            },
            method: 'POST',
            success: function (res) {
        
              if (res.data.result == true) {
              
                //0是别人登陆 1是商家登陆 2个人登陆
                wx.setStorageSync("type", res.data.object.type)
                //本地存储
                wx.setStorageSync("token", res.data.data)
                wx.setStorageSync("phone", res.data.object.phone)
                //openId
                wx.setStorageSync("openId", res.data.body.openid)
                //城市Id
                //codeNum
                // wx.setStorageSync("codeNum", res.data.code)
                wx.setStorageSync("userMess", res.data.object)
                // wx.setStorageSync("balance", res.data.object.balance)
                if (wx.getStorageSync("userId")!=''){
                  that.empower()
                }
                if (res.data.object.type == -1) {
                  wx.navigateTo({
                    url: '/pages/sf_xz/sf_xz',
                  })
                } else {
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 1500,
                    success: () => {
                      wx.reLaunch({
                        url: '/pages/index/index',
                      })
                    }
                  })
                }
                

              } else if (res.data.result == false) {
                //codeNum
                // wx.setStorageSync("codeNum", res.data.code)
                wx.setStorageSync("phone", res.data.object.phone)
                //本地存储
                wx.setStorageSync("token", res.data.data)
                
                wx.setStorageSync("userMess", res.data.object)
                //openId
                wx.setStorageSync("openId", res.data.body.openid)

                if (res.data.object.type==-1){
                 
                  wx.navigateTo({
                    url: '/pages/sf_xz/sf_xz',
                  })
                }else{
                  wx.reLaunch({
                    url: '/pages/index/index',
                  })
                }
              }

            }
          })
        }
      })
      
       
      // wx.getStorage('loginFlag', true)
    }else{

    }

  }),

  //手机验证码登陆
  tapLogin: function () {

    wx.navigateTo({
      url: '../loginPhone/loginPhone?city=' + this.data.city + '&province=' + this.data.province + '&avatarUrl=' + this.data.avatarUrl + '&gender=' + this.data.gender + '&nickName=' + this.data.nickName + '&referee=' + this.data.referee + '&cityId=' + this.data.cityId,
    })
  },

//登陆
  loginPhone: function (code){
    let that=this
    

    },
    empower: function(res){
     
      api.empower({
        grantUserId: wx.getStorageSync("userId"),
        userId: wx.getStorageSync("userMess").id,
      }).then(res => {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      })
    }

  
    

})